package com.linewell.controller;

import com.linewell.entity.Administrator;
import com.linewell.entity.Article;
import com.linewell.entity.Tag;
import com.linewell.entity.UserInfo;
import com.linewell.service.IArticleService;
import com.linewell.service.ITagService;
import com.linewell.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Random;

/**
 * @Title: ssm2
 * @description: 主页面管理控制器
 * @author: hyuhuang@linewell.com
 * @since:2018/1/18
 */
@Controller
@SessionAttributes("LoginUser") //设置一个Session，LoginUser，调用model方法将自己存入Session
public class HomeController {

	@Autowired
    IUserService userService;
	@Autowired
    IArticleService articleService;
	@Autowired
	ITagService	tagService;

		//登录管理
	/**
	 * @descripition  登录页面跳转管理
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:47
	 * @param punsterName, password, but, model
	 * @return 跳转页面
	 */
	@RequestMapping("/login")
	public String choice(String punsterName,String password, String but,Model model){
	                 if (("0").equals(but)) {
						if (punsterName == "" || password == ""){
							model.addAttribute("list","用户名或者密码不能为空");
							return "Init";
						} else {
							List<UserInfo> list = userService.checkNamePwd(punsterName, password);
							Administrator admin = userService.checkAdmin(punsterName,password);
							if (list.size() > 0) {
								UserInfo userSession = list.get(0);
								if (userSession.getState() == 0) {
									List<Tag> tagList = tagService.getHotTag();
									model.addAttribute("tagList",tagList);
									model.addAttribute("LoginUser", userSession);
									return "home";
								} else {
									model.addAttribute("list", "您的账号涉嫌违规，被封禁。如有疑问咨询管理员");
									return "Init";
								}

							} else if (admin != null){
								if ( 0 == admin.getType()){
									model.addAttribute("LoginUser",admin);
									return "admin";
								} else if (1 == admin.getType()){
									model.addAttribute("LoginUser",admin);
									return "editor";
								}
							} else {
                                model.addAttribute("list", "用户名或密码错误");
                                return "Init";
							}
						}
					 } else if (("1").equals(but)) {
						return "register";
					 }else if (("2").equals(but)) {
						return "youke";
					 }
				 return null;
		}



	/**
	* @descripition 跳转到主页面
	* @author hyuhuang@linewell.com
	* @date 2018/5/5
	* @param requestmodel
	* @return java.lang.String
	*/
	@RequestMapping("/home")
    public String goHome(HttpServletRequest request, Model model){
	    if(request.getSession() == null){
	        return "Init";
        }else {
            List<Article> articleList = articleService.selectAllArticle();
            List<Tag> tagList = tagService.getHotTag();
            model.addAttribute("tagList",tagList);
            model.addAttribute("articleList", articleList);
            return "home";
        }

    }
	/**
	 * @descripition  生成随机验证码图片
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:49
	 * @param request, response, model
	 */
	@RequestMapping("/Verification_Code")//@RequestParam
	public void getVerificationCode(HttpServletRequest request, HttpServletResponse response,Model model) throws Exception{
		//禁止浏览器缓存随机图片
		response.setDateHeader("Expires", -1);
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");

		//通知客户机以图片方式打开发送过去的数据
		response.setHeader("Content-Type","image/jpeg");

		//在内存中创建一副图片
		BufferedImage image = new BufferedImage(90,40,BufferedImage.TYPE_INT_RGB);

		Graphics g = image.getGraphics();

		//设背景色
		g.setColor(Color.gray);
		g.fillRect(0,0,90,40);

		//随机产生120个干扰点
		for(int i=0;i<120;i++){
			int x=(int)(Math.random()*90);
			int y=(int)(Math.random()*40);
			g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
			//画圆点
			g.drawOval(x, y, 1, 0);
		}

		//随机生成数值，保存到session
		String codeValue="0123456789ABCDEFGHIJkLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		char value[]=codeValue.toCharArray();
		Random random=new Random();
		char[] code=new char[4];
		for(int i = 0;i < 4; i++){
			code[i]=value[random.nextInt(value.length)];
		}
		request.getSession().setAttribute("code",String.valueOf(code));

		//设置数据字体
		g.setFont(new Font(null,Font.BOLD,30));
		g.setColor(Color.black);

		//向图片上写数据
		for(int i=0;i<code.length;i++){
			g.drawString(""+code[i],i*18+10,30);
		}

		//把写好数据的图片输出给浏览器
		ImageIO.write(image,"jpg",response.getOutputStream());
	}
	/**
	 * @descripition 指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向writer.jsp
	 */
	@RequestMapping("/writer")
	public String writer(){ return "writer";}

	/**
	 * @descripition 指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向information.jsp
	 */
	@RequestMapping("/myInfo")
	public String information(){
		return "userInfo";
	}

	/**
	 * @descripition 指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向Init.jsp
	 */
	@RequestMapping("/")
	public String Login(){
		return "Init";
	}
	/**
	 * @descripition  指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向Init.jsp
	 */
	@RequestMapping("/layout")
	public String layout(){
		return "Init";
	}
	/**
	 * @descripition 指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向Leaderboard.jsp
	 */
	@RequestMapping("/Leaderboard")
	public String Leaderboard(){
		return "leaderboard";
	}
	/**
	 * @descripition 指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向articleManage.jsp
	 */
	@RequestMapping("/articleManage")
	public String articleManage(){
		return "articleManage";
	}
	/**
	 * @descripition 指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向message.jsp
	 */
	@RequestMapping("/messageManage")
	public String messageManage(){
		return "message";
	}
	/**
	 * @descripition 指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向recommend.jsp
	 */
	@RequestMapping("/recommend")
	public String recommend(){
		return "recommend";
	}
	/**
	 * @descripition 指定访问该控制器的url
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:46
	 * @return 指定转向recoverPwd.jsp
	 */
    @RequestMapping("/recoverPwd")
    public String recoverPwd(){
        return "recoverPwd";
    }
}
